/****************************************************************************
 *  _____       ______  _____
 * |_   _|     |  ____|/ ____|
 *   | |  _ __ | |__  | (___    Institute of Embedded Systems
 *   | | | '_ \|  __|  \___ \   Zürcher Hochschule für Angewandte
 *  _| |_| | | | |____ ____) |  Wissenschaften
 * |_____|_| |_|______|_____/   8401 Winterthur, Switzerland
 *
 ****************************************************************************
 *
 * @version $Rev: 431 $
 * @author  $Author: mcrescenti $
 * @date    $Date: 2014-06-16 15:46:42 +0200 (lun., 16 juin 2014) $
 *
 ***************************************************************************/

#ifndef __PPS_PLL_H__
#define __PPS_PLL_H__

#include <stdint.h>
#include <stdbool.h>


#ifndef __PTP2_MASTER_T_H__
#define __PTP2_MASTER_T_H__
typedef struct _ptp2_master_t ptp2_master_t;
#endif



typedef struct {
    bool                stable;                 // true when the PPS reference is esteemed stable
    int32_t             delta_1pps;             // retrieved from the 1PPS-PLL block
    int32_t             avg_delta_1pps;         // averaged delta_1pps
    int32_t             last_delta_1pps;        // previous 1pps delta value
    uint32_t            dpll_period;            // retrieved from the 1PPS-PLL block
    uint32_t            last_phase_buildout;    // amplitude of last phase buildout
    uint32_t            buildout_num;
    uint32_t            pps_width;              // PPS out duration 8ns x digit
    uint32_t            accumulator;
    uint32_t            lim_tmin;
    uint32_t            lim_tmax;
    uint32_t            limit_counter;          // counter is incremented when limits are reached
    uint32_t            ovf_counter;            // counter is incremented when OVF bit is set
} pps_pll_t;



extern bool pps_pll_init(pps_pll_t *pps_pll, ptp2_master_t *master);
extern void pps_pll_monitor(pps_pll_t *pps_pll);

#endif // __PPS_PLL_H__
